import {
  Body,
  Controller,
  Get,
  Param,
  Headers,
  Query,
  Post,
  Put,
  Delete,
  UseInterceptors,
  ClassSerializerInterceptor,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { IsAuth } from 'src/decorators/auth.decorator';
import { IResponseData, responseData } from 'src/utils/result';
import { PositionService } from './position.service';
import {
  CreatePositionDto,
  FindPositionListDto,
  PositionDto,
  UpdatePositionDto,
} from './position.dto';

@ApiTags('岗位模块')
@ApiBearerAuth()
@Controller('position')
export class PositionController {
  constructor(private readonly positionService: PositionService) {}
  @ApiOperation({ summary: '查询岗位列表' })
  @ApiResponse({
    status: 200,
    description: '查询岗位列表',
    type: PositionDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get()
  @IsAuth('system:position:list')
  async findList(
    @Query() dto: FindPositionListDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.positionService.getLists({
      where: {
        ...dto,
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询所有岗位' })
  @ApiResponse({
    status: 200,
    description: '查询所有岗位',
    type: PositionDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get('findAll')
  async findAll(@Headers() headers): Promise<IResponseData> {
    const res = await this.positionService.getAll();
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询岗位详情' })
  @ApiResponse({
    status: 200,
    description: '查询岗位详情',
    type: PositionDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get(':id')
  @IsAuth('system:position:detail')
  async find(
    @Param('id') id: number,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.positionService.getOne({
      where: {
        id,
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @Post()
  @IsAuth('system:position:add')
  @ApiOperation({ summary: '创建岗位' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async create(
    @Body() dto: CreatePositionDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.positionService.create(dto);
    return responseData({
      code: 100020,
      data: res,
      language: headers['language'],
    });
  }

  @Put()
  @IsAuth('system:position:update')
  @ApiOperation({ summary: '更新岗位' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async update(
    @Body() dto: UpdatePositionDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.positionService.update(dto);
    return responseData({
      code: 100030,
      data: res,
      language: headers['language'],
    });
  }

  @Delete()
  @IsAuth('system:position:delete')
  @ApiOperation({ summary: '删除岗位' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async delete(
    @Query('ids') query: string,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.positionService.delete(query);
    return responseData({
      code: 100040,
      data: res,
      language: headers['language'],
    });
  }
}
